﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhsystem.Main;
using dhSystemBLL.orderData;
using baseSet.importExport;
using baseSet.power;
using dhDataAcess.dataAcess;

namespace dhsystem.orderForm
{
    public partial class FrmMaterialList : Form
    {
        materialListBLL materialList = new materialListBLL();
        string state;
        string type;
        public FrmMaterialList(string _type)
        {
            InitializeComponent();
            type = _type;
        }
        //新增按钮，显示新增窗口
        private void toolAdd_Click(object sender, EventArgs e)
        {
            state = "新增";
            FrmMaterialAdd materialAdd = new FrmMaterialAdd(bindingSourceMaterialList  ,state,type  );
            materialAdd.ShowDialog();
        }
        //打开窗口时显示列表数据
        private void FrmMaterialList_Load(object sender, EventArgs e)
        {
            if (type == "原材料")
            {
                bindingSourceMaterialList.DataSource = materialList.getmaterialListBymtypeNoAll();
                dGridMaterialList.DataSource = bindingSourceMaterialList;
            }
            else
            {
                bindingSourceMaterialList.DataSource = materialList.getmaterialListBymytypeAll  ();
                dGridMaterialList.DataSource = bindingSourceMaterialList;
            }
            DcolumnSet.colSet(moduleInfo.materialList , dGridMaterialList );
            toolStripStatusLabel1.Text = "数据总条数:" + dGridMaterialList.RowCount.ToString() + "条";

            sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.materialList );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                buttImport.Enabled = modulAaccessRow.importAc;

            }
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.materialList );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridMaterialList.DataSource = query.rTable;
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            state = "编辑";
            FrmMaterialAdd materialAdd = new FrmMaterialAdd(bindingSourceMaterialList, state,type );
            materialAdd.ShowDialog();
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string matNum = dGridMaterialList.Rows[dGridMaterialList.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除原材料：" + matNum + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceMaterialList.DataSource = materialList.materialListDel(matNum);
                    dGridMaterialList.DataSource = bindingSourceMaterialList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            if (type == "原材料")
            {
                bindingSourceMaterialList.DataSource = materialList.getmaterialListBymtypeNoAll ();
                DcolumnSet.colSet(moduleInfo.materialList, dGridMaterialList);
                toolStripStatusLabel1.Text = "数据总条数:" + dGridMaterialList.RowCount.ToString() + "条";
            }
            else
            {
                bindingSourceMaterialList.DataSource = materialList.getmaterialListBymytypeAll ();
                DcolumnSet.colSet(moduleInfo.materialList, dGridMaterialList);
                toolStripStatusLabel1.Text = "数据总条数:" + dGridMaterialList.RowCount.ToString() + "条";
            }
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (type == "原材料")
                {
                    bindingSourceMaterialList.DataSource = materialList.getmaterialListBymatNamelikeNoAll("%" + textMatNameQuery.Text + "%");
                    dGridMaterialList.DataSource = bindingSourceMaterialList;
                }
                else
                {
                    bindingSourceMaterialList.DataSource = materialList.getmaterialListBymatNamelikeAll ("%" + textMatNameQuery.Text + "%");
                    dGridMaterialList.DataSource = bindingSourceMaterialList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
            if (type == "原材料")
            {
                bindingSourceMaterialList.DataSource = materialList.getmaterialListBymtypeNoAll();
                dGridMaterialList.DataSource = bindingSourceMaterialList;
            }
            else
            {
                bindingSourceMaterialList.DataSource = materialList.getmaterialListBymytypeAll ();
                dGridMaterialList.DataSource = bindingSourceMaterialList;
            }
            DcolumnSet.colSet(moduleInfo.materialList, dGridMaterialList);
            toolStripStatusLabel1.Text = "数据总条数:" + dGridMaterialList.RowCount.ToString() + "条";
        }
        //工具栏退出按钮，退出窗口
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //导入按钮
        private void buttImport_Click(object sender, EventArgs e)
        {
            FrmImport import = new FrmImport(moduleInfo.materialList);
            import.ShowDialog();
        }
        //datagridview第一列显示行号
        private void dGridMaterialList_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.materialList , dGridMaterialList , "原材料信息");
            frm.ShowDialog();
        }

        private void toolSet_Click(object sender, EventArgs e)
        {

        }
    }
}
